﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using AutoMapper;
using Library.Common.Helper;
namespace Library.ServiceExtensions.AutoMapper
{
    public static class AutoMapperSetUp
    {
        public static IServiceCollection AddAutoMapperSetUp(this IServiceCollection serviceDescriptors)
        {
            var assembly = AssemblyHelper.LoadByNameEndString($"LogForParameter.IServices");
            var config = new MapperConfiguration(cfg => {
                var types = assembly.GetTypes().Where(m => typeof(IMapperConfig).IsAssignableFrom(m));

                foreach (var type in types)
                {
                    ((IMapperConfig)Activator.CreateInstance(type)).Bind(cfg);
                }
            });
            serviceDescriptors.AddSingleton(config.CreateMapper());
            return serviceDescriptors;
        }
    }
}
